package com.rh.config;

import com.rh.filter.TokenAuthenticationFilter;
import com.rh.filter.TokenLoginFilter;
import com.rh.security.DefaultPasswordEncoder;
import com.rh.security.TokenLogoutHandler;
import com.rh.security.TokenManager;
import com.rh.security.UnauthorizedEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * Security核心配置类
 */
@Configuration
@EnableWebSecurity  //SpringSecurity自动装配注解
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启注解功能
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {

    private UserDetailsService userDetailsService;//查询数据库的实现类的接口
    private TokenManager tokenManager; //token管理器器
    private DefaultPasswordEncoder defaultPasswordEncoder; //密码处理器
    private RedisTemplate redisTemplate; //操作redis模板

    //有参构造
    @Autowired//参数要做一个注入
    public TokenWebSecurityConfig(UserDetailsService userDetailsService, DefaultPasswordEncoder defaultPasswordEncoder,
                                  TokenManager tokenManager, RedisTemplate redisTemplate) {
        this.userDetailsService = userDetailsService;
        this.defaultPasswordEncoder = defaultPasswordEncoder;
        this.tokenManager = tokenManager;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 核心配置设置
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new UnauthorizedEntryPoint())//没权限访问时,进入没权限统一处理器
                .and().csrf().disable()//关闭CSRF
                .authorizeRequests()//请求
                .anyRequest().authenticated()//权限
                .and().logout().logoutUrl("/admin/acl/index/logout")//设置退出路径
                .addLogoutHandler(new TokenLogoutHandler(tokenManager,redisTemplate)).and()//退出执行退出处理器里面的方法
                //添加自定义认证、授权过滤器
                .addFilter(new TokenLoginFilter(authenticationManager(), tokenManager, redisTemplate))
                .addFilter(new TokenAuthenticationFilter(authenticationManager(), tokenManager, redisTemplate)).httpBasic();
    }
    /**
     * 密码处理
     * @param auth
     * @throws Exception
     */
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordEncoder);
    }

    /**
     * 配置哪些请求不拦截
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        //设置哪些路径不拦截
        web.ignoring().antMatchers("/api/**",
                "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
    }
}